package com.ran.sys.entity.controller;

import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ran.common.vo.Result;
import com.ran.sys.entity.User;
import com.ran.sys.mapper.UserMapper;
import com.ran.sys.service.IFaceService;
import com.ran.sys.service.IUserService;
import com.ran.sys.service.impl.FaceServiceImpl;
import com.ran.sys.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;


@Api
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private FaceServiceImpl faceService;

    @Autowired
    private BaseMapper<User> baseMapper;

    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody User user) {
        User dbuser=null;
        try{
            dbuser = userService.getUserInfoByUserName(user.getUsername());
        }catch (Exception e){
            return Result.fail("没找到该用户");
        }
        Map<String, Object> data = userService.userlogin(dbuser, user);
        if (data == null) {
            return Result.fail("密码错误!");
        }
        return Result.success(data, user.getUsername() + "登录成功！");
    }

    @PostMapping("/register")
    public Result<Map<String, Object>> register(@RequestBody User user) {
        //判定该用户是否已经注册
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User loginUser = this.baseMapper.selectOne(wrapper);
        //加密
        if (loginUser!=null){
            return Result.fail("已经存在此用户");
        }
        User user2 = userService.userregister(user);
        //存入数据库
        boolean is_Success = userService.save(user2);
        if (is_Success) {
            return Result.success();
        } else {
            return Result.fail("注册操作失败");
        }
    }

    //修改账号和密码
    @PostMapping("/update")
    public Result<Map<String, Object>> update(@RequestBody User user,
                                              HttpServletRequest request) {
        User updateUser = userService.userUpdate(request, user);
        if (updateUser == null) {
            return Result.fail("token有问题，空或错误");
        }
        boolean is_Success = userService.updateById(updateUser);
        if (is_Success) {
            return Result.success();
        } else {
            return Result.fail("");
        }
    }

    //上传人脸，储存特征
        @PostMapping("/face")
        public Result<Map<String, Object>> face(@RequestBody MultipartFile file,
                HttpServletRequest request) {
            User user = faceService.faceToken(request);
            if (user == null) {
                return Result.fail("token有问题，空或错误");
            }
            //以上检测token
            File targetFile = faceService.dealFile(file);
            if (targetFile == null) {
                return Result.fail("file为空！");
            }
            //以上处理上传文件，存到本地路径targetFile
            String vector = faceService.sendRequest(targetFile, user);
            //以上发送请求
            boolean success = faceService.insertFace(targetFile, user,vector);
            if (success) {
                return Result.success();
            } else {
                return Result.fail("添加失败！");
            }

    }

    //人脸识别
    @PostMapping("/facecheck")
    public Result<Map<String, Object>> facecheck(@RequestParam MultipartFile file,
                                                 HttpServletRequest request) {
        User user = faceService.faceToken(request);
        if (user == null) {
            return Result.fail("token有问题，空或错误");
        }
        File targetFile = faceService.dealFile(file);
        if (targetFile == null) {
            return Result.fail("file为空！");
        }
        String template1 = faceService.getTemplate(user);
        if (template1 == null) {
            return Result.fail("数据库中的template为空！");
        }//获取在数据库中存储为String的128个数字（一条很长的字符串）
        Map<String, Object> obj = faceService.sendRequest2(targetFile, user, template1);
        if (obj.isEmpty()){
            return Result.fail();
        }else
            return Result.success(obj);


    }
}
